package org.example.Frame;

import org.example.Pojo.User;
import org.example.Util.FileUtil;
import org.example.implement.MyFrame;

import javax.swing.*;
import javax.swing.tree.MutableTreeNode;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Map;
import java.util.Vector;

public class Score extends JFrame implements MyFrame {
    private Vector<User>playScore;//一个人的成绩
    private User player;//现在的玩家
    private int rank;
    //对本玩家的文件进行读取后排序显示

    public Score(int rank,User player) throws HeadlessException {
        //界面初始化
        this.player = player;
        this.setTitle("成绩展示");
        this.setSize(400,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //成绩初始化
        try {
            playScore= FileUtil.ReadplayScore(player);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        this.rank=rank;
        //绘画板初始化
        JPanel panel=new JPanel();
        draw(panel);
        this.add(panel);
        this.setVisible(true);
        //关闭按键重写
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                try {
                    FileUtil.DeleteandWrite(player,playScore);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }

    @Override
    public void draw(JPanel panel) {
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        //本用户的信息读取
        JPanel thisPanel=new JPanel();
        thisPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel thislabel=new JLabel("用户名:"+player.getName()+" "+"耗时:"+player.getLocalTime()+" "+"总分:"+player.getScore()+"排名"+rank);
        thislabel.setFont(new Font("宋体",Font.PLAIN,12));
        thisPanel.add(thislabel);
        panel.add(thisPanel);
        JFrame frame=this;
        //本用户所有成绩
        for(User t:playScore){
            JPanel dataPanel=new JPanel();
            dataPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            JLabel datalabel=new JLabel("用户名:"+t.getName()+" "+t.getScore()+" "+"耗时:"+t.getLocalTime()+" "+"总分:"+t.getScore());
            datalabel.setFont(new Font("宋体",Font.PLAIN,12));
            dataPanel.add(datalabel);
            //删除按钮设置
            JButton deleteButton=new JButton("删除");
            deleteButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dataPanel.removeAll();
                    frame.revalidate();
                    frame.repaint();
                }
            });
            dataPanel.add(deleteButton);
            panel.add(dataPanel);
        }

    }

    @Override
    public void close() {

    }
}
